package io.renren.modules.app.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.app.entity.TradeRecordingEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.entity.vo.QueryWithdrawalsParam;
import io.renren.modules.app.entity.vo.SelectTradeData;
import io.renren.modules.app.entity.vo.SelectWithdrawalsData;
import io.renren.modules.app.service.UserService;
import io.renren.modules.app.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.app.dao.WithdrawalsRecordingDao;
import io.renren.modules.app.entity.WithdrawalsRecordingEntity;
import io.renren.modules.app.service.WithdrawalsRecordingService;
import org.springframework.transaction.annotation.Transactional;


@Service("withdrawalsRecordingService")
public class WithdrawalsRecordingServiceImpl extends ServiceImpl<WithdrawalsRecordingDao, WithdrawalsRecordingEntity> implements WithdrawalsRecordingService {

    @Autowired
    private Snowflake snowflake;

    @Autowired
    private UserService userService;


    @Override
    public SelectWithdrawalsData queryWithdrawalsRecording(QueryWithdrawalsParam withdrawalsParam) {
        QueryWrapper<WithdrawalsRecordingEntity> wrapper = new QueryWrapper<>();
        if (withdrawalsParam.getUserId() != null) {
            wrapper.eq("user_id", withdrawalsParam.getUserId());
        }
        if (StringUtils.isNotEmpty(withdrawalsParam.getWithdrawalsNo())) {
            wrapper.eq("withdrawals_no", withdrawalsParam.getWithdrawalsNo());
        }
        if (StringUtils.isNotEmpty(withdrawalsParam.getCardNumber())) {
            wrapper.eq("card_number", withdrawalsParam.getCardNumber());
        }
        if (StringUtils.isNotEmpty(withdrawalsParam.getPhone())) {
            wrapper.eq("phone", withdrawalsParam.getPhone());
        }
        if (StringUtils.isNotNull(withdrawalsParam.getAuditStatus())) {
            wrapper.eq("audit_status", withdrawalsParam.getAuditStatus());
        }
        wrapper.orderByDesc("create_time");
        Page<WithdrawalsRecordingEntity> iPage = new Page<>(withdrawalsParam.getPageNum(), withdrawalsParam.getPageSize());
        IPage<WithdrawalsRecordingEntity> cookBookIPage = baseMapper.selectPage(iPage, wrapper);
        List<WithdrawalsRecordingEntity> list = cookBookIPage.getRecords();
        for (WithdrawalsRecordingEntity recordingEntity : list) {
            UserEntity userEntity = new UserEntity();
            userEntity.setUserId(recordingEntity.getUserId());
            UserEntity user = userService.queryUserDetails(userEntity);
            recordingEntity.setUserName(user.getUsername());
        }

        SelectWithdrawalsData data = new SelectWithdrawalsData();
        data.setTotal(cookBookIPage.getTotal());
        data.setCurrentIndex(cookBookIPage.getCurrent());
        data.setSize(cookBookIPage.getSize());
        data.setPageCount(cookBookIPage.getPages());
        data.setWithdrawalsRecordingList(list);
        return data;
    }


    @Transactional
    @Override
    public Integer addWithdrawalsRecording(WithdrawalsRecordingEntity withdrawalsRecording) {
        //生成提现单号
        String withdrawalsNo = String.valueOf(snowflake.nextId());
        withdrawalsRecording.setWithdrawalsNo("WI" + withdrawalsNo);
        withdrawalsRecording.setCreateTime(new Date());
        return baseMapper.insert(withdrawalsRecording);
    }

}